import { takeEvery } from 'redux-saga';
import { put, call } from 'redux-saga/effects'
import { LOAD, LOAD_SUCCESS, LOAD_FAIL } from './constants';
// import api from '../../utils/ApiClient'
// import request from '../../utils/request'
// import {pro, super_get}  from '../../utils/super'
import {ax_get} from '../../utils/ax_agent'

export default function* postSaga() {
  yield [
    getPostData()
  ]
}

function* fetchPost(action) {
   try {
      // const result = yield call( request,  '/api/post');
      // const result = yield call( pro,  '/api/post')
      // const result = yield call( super_get,  '/api/post')

      const result = yield call( ax_get,  '/api/post', {params: {page: action.page_number}} )

      yield put({type: LOAD_SUCCESS, result: result.data});
   } catch (e) {
      yield put({type: LOAD_FAIL, message: e.message});
   }
}

function* getPostData() {
  yield* takeEvery( LOAD, fetchPost)
}